package com.sservice.commonBusiness.service.impl;

import com.sservice.base.utils.service.SService;
import com.sservice.commonBusiness.e.Statistic;
import com.sservice.commonBusiness.mapper.StatisticMapper;
import com.sservice.commonBusiness.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;

/**
 * Created by byte_su@163.com on 2018/02/05.
 */
@Service
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private StatisticMapper statisticMapper;

    @Override
    public Statistic selectBy(String targetId, String type1) {
        Statistic statistic = new Statistic();
        statistic.setType1(type1);
        statistic.setTargetId(targetId);
        Statistic statistic1 = statisticMapper.selectBy(statistic);
        if (statistic1 == null) {
            statisticMapper.insertSelective(statistic);
            statistic1 = statistic;
        }
        return statistic1;
    }

    /**
     * 记录统计
     *
     * @param targetId
     * @param type1
     * @param fieldName
     * @param fieldValue
     * @return
     */
    public int record(String targetId, String type1, String fieldName, BigDecimal fieldValue) {
        Statistic statistic = new Statistic();
        statistic.setTargetId(targetId);
        statistic.setType1(type1);
        Class clazz = statistic.getClass();

        PropertyDescriptor pd = null;
        Method setMethod = null;
        int res = 0;
        try {
            pd = new PropertyDescriptor(fieldName, clazz);
            setMethod = pd.getWriteMethod();
            setMethod.invoke(statistic, fieldValue);
            res = statisticMapper.insertSelective(statistic);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    /**
     * 统计信息
     *
     * @param targetId
     * @param type1
     * @param fieldName
     * @return
     */
    @Override
    public int plus(String targetId, String type1, String fieldName) {
        Statistic statistic = new Statistic();
        statistic.setTargetId(targetId);
        statistic.setType1(type1);
        Statistic sta = selectBy(statistic.getTargetId(), statistic.getType1());
        if (sta == null) {
            statisticMapper.insertSelective(statistic);

        } else {
            statistic.setId(sta.getId());

        }
        Class clazz = statistic.getClass();

        PropertyDescriptor pd = null;
        Method setMethod = null;
        Method getMethod = null;
        int res = 0;
        try {
            pd = new PropertyDescriptor(fieldName, clazz);
            setMethod = pd.getWriteMethod();
            getMethod = pd.getReadMethod();
            BigDecimal o = (BigDecimal) getMethod.invoke(sta);
            o = o == null ? BigDecimal.ZERO : o;
            setMethod.invoke(statistic, o.add(BigDecimal.ONE));
            res = statisticMapper.updateByPrimaryKeySelective(statistic);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    public int setField(Statistic statistic) {
        Statistic sta = selectBy(statistic.getTargetId(), statistic.getType1());
        if (sta == null) {
            return statisticMapper.insertSelective(statistic);
        } else {
            statistic.setId(sta.getId());
            return  statisticMapper.updateByPrimaryKeySelective(statistic);
        }
    }

    /**
     * 综合评分记录
     *
     * @param targetId      // 目标ID
     * @param type1         统计类型
     * @param thisTimeScore 本次评分 多个用":"分隔
     * @return
     */
    @Override
    public int genScore(String targetId, String type1, String thisTimeScore) {
        Statistic statistic = selectBy(targetId, type1);
        if (statistic == null) {
            statistic = new Statistic();
            statistic.setTargetId(targetId);
            statistic.setType1(type1);
            statistic.setScore(SService.genScore(null, thisTimeScore));
            return statisticMapper.insertSelective(statistic);
        } else {
            statistic.setScore(SService.genScore(statistic.getScore(), thisTimeScore));
            return statisticMapper.updateByPrimaryKeySelective(statistic);
        }
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return statisticMapper.deleteByPrimaryKey(id);
    }


    public int insertSelective(Statistic record) {
        return statisticMapper.insertSelective(record);
    }

    public Statistic selectByPrimaryKey(Integer id) {
        return statisticMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(Statistic record) {
        return statisticMapper.updateByPrimaryKeySelective(record);
    }
}
